package com.example.sqliteapplication.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * Created by wly on 2019/4/25.
 */

public class AddressDBUtils {
    public static String  findaddress(Context context,String phone){
        SQLiteDatabase db=SQLiteDatabase.openDatabase(context.getFilesDir()+"/address.db",null,SQLiteDatabase.OPEN_READONLY);


        String regex="1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\\d{8}";
        String address="";
        if (phone.matches(regex)){
            //手机号
            phone=phone.substring(0,7);
            Cursor cursor=db.query("info",new String[]{"cardtype"},"mobileprefix=?",new String[]{phone},null,null,null);
            //Log.i("Main", "findall: ok"+cursor.getCount());

            if (cursor!=null&&cursor.getCount()>0){
                cursor.moveToNext();
                address=cursor.getString(0);
                cursor.close();
            }
        }else{
            switch (phone.length()){
                case 3:
                    address="紧急电话";
                    break;
                case 5:
                    address="运营商";
                    break;
                case 7:
                case 8:
                        address="本地座机";
                        break;
                case 11:
                    address="长途电话";
                    break;
                default:
                    address="未知号码";



            }

        }


        return address;
    }

}
